package com.hnzq.cloud.commons;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class TransferBeanUtil {

    public static <T>T copyObject(Object source,Class<T> type){

        T t= null;
        try {
            t = type.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        BeanUtils.copyProperties(source,t);

        return t;
    }

    public static <T>List<T> copyList(@SuppressWarnings("rawtTypes") Collection source, Class<T> type){

        ArrayList<T> list = new ArrayList<>();
        for (Object o : source) {
            T t =null;
            try {
                t = type.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            BeanUtils.copyProperties(o,t);
            list.add(t);
        }
        return list;
    }
}
